package com.bzy.blp.upms.service.impl;

import com.bzy.blp.mapper.TSystemResourceInfoDatMapper;
import com.bzy.blp.mapper.TSystemRoleInfoDatMapper;
import com.bzy.blp.mapper.TSystemRoleResourceRltMapper;
import com.bzy.blp.mapper.TSystemUserRoleRltMapper;
import com.bzy.blp.model.*;
import com.bzy.blp.upms.service.RoleService;
import com.bzy.common.exception.LogicException;
import com.bzy.common.exception.SqlException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Service
public class RoleServiceImpl implements RoleService {

    private Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Autowired
    TSystemUserRoleRltMapper tSystemUserRoleRltMapper;
    @Autowired
    TSystemRoleInfoDatMapper tSystemRoleInfoDatMapper;
    @Autowired
    TSystemRoleResourceRltMapper tSystemRoleResourceRltMapper;
    @Autowired
    TSystemResourceInfoDatMapper tSystemResourceInfoDatMapper;

    @Override
    public TSystemRoleInfoDat findOne(Long roleId) throws Exception {
        return tSystemRoleInfoDatMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public Set<String> findRoles(Long userId, Long appId) throws Exception {
        List<TSystemUserRoleRlt> tSystemUserRoleRlts = queryUserRoleList(userId, appId);
        Set<String> roles = new HashSet<String>();
        for (TSystemUserRoleRlt node : tSystemUserRoleRlts) {
            TSystemRoleInfoDat roleInfo = findOne(node.getRoleId());
            roles.add(roleInfo.getRoleName());
        }
        return roles;
    }

    @Override
    public Set<String> findPermissions(Long userId, Long appId) throws Exception {

        Set<String> permissions = new HashSet<String>();
        List<TSystemUserRoleRlt> dataList = queryUserRoleList(userId, appId);
        for (TSystemUserRoleRlt node : dataList) {
            Long roleId = node.getRoleId();

            // 查找资源 id
            TSystemRoleResourceRltCriteria criteria = new TSystemRoleResourceRltCriteria();
            criteria.createCriteria().andRoleIdEqualTo(roleId.intValue());
            List<TSystemRoleResourceRlt> dataList2 = tSystemRoleResourceRltMapper.selectByExample(criteria);
            for (TSystemRoleResourceRlt node2 : dataList2) {
                Integer resourceId = node2.getResourceId();
                TSystemResourceInfoDat data2 = tSystemResourceInfoDatMapper.selectByPrimaryKey(Long.valueOf(resourceId));
                if (data2 == null) {
                    logger.info("RoleServiceImpl.java--findPermissions--resource is not exist--"+resourceId);
                } else {
                    if (StringUtils.isBlank(data2.getPermissionsCode()))
                        logger.info("RoleServiceImpl.java --findPermissions --permissions_code is empty");
                    else
                        permissions.add(data2.getPermissionsCode());
                }
            }
        }
        return permissions;
    }


    @Override
    public Long findRoleIdByUserIdAndAppId(Long userId, Long appId) throws Exception {
        if (appId == null)
            throw new LogicException("appId 不能为空");
        List<TSystemUserRoleRlt> roleList = queryUserRoleList(userId, appId);
        if (roleList == null)
            throw new SqlException("未找到角色信息");
        if (roleList.size() > 1)
            throw new LogicException("角色信息只能有一个");
        return roleList.get(0).getRoleId();
    }

    @Override
    public List<Long> findRoleIdListByAppId(Long appId) throws Exception {
        List<Long> roleIds = new ArrayList<>();
        if (appId == null)
            throw new LogicException("appId 不能为空");
        List<TSystemUserRoleRlt> roleList = queryUserRoleList(null,appId);
        if (roleList == null)
            throw new SqlException("未找到角色信息");
        roleList.forEach(userRole -> {
            roleIds.add(userRole.getRoleId());
        });
        return roleIds;
    }


    private List<TSystemUserRoleRlt> queryUserRoleList(Long userId, Long appId) throws Exception {
        TSystemUserRoleRltCriteria criteria = new TSystemUserRoleRltCriteria();
        if (userId != null) {
            criteria.createCriteria().andUserIdEqualTo(userId);
        }
        if (appId != null) {
            criteria.createCriteria().andAppIdEqualTo(appId);
        }
        return tSystemUserRoleRltMapper.selectByExample(criteria);
    }
}
